<?php

// 批量更新所有表单字段的中文标签

$formsToUpdate = [
    // Categories表单
    'app/Filament/Resources/Categories/Schemas/CategoryForm.php' => [
        "TextInput::make('name')" => "TextInput::make('name')\n                    ->label('名称')",
        "TextInput::make('slug')" => "TextInput::make('slug')\n                    ->label('别名')",
        "Textarea::make('description')" => "Textarea::make('description')\n                    ->label('描述')",
        "FileUpload::make('image')" => "FileUpload::make('image')\n                    ->label('图片')",
        "Select::make('parent_id')" => "Select::make('parent_id')\n                    ->label('父级分类')",
        "TextInput::make('sort_order')" => "TextInput::make('sort_order')\n                    ->label('排序')",
        "Toggle::make('is_active')" => "Toggle::make('is_active')\n                    ->label('启用')",
    ],

    // Tags表单
    'app/Filament/Resources/Tags/Schemas/TagForm.php' => [
        "TextInput::make('name')" => "TextInput::make('name')\n                    ->label('名称')",
        "TextInput::make('slug')" => "TextInput::make('slug')\n                    ->label('别名')",
        "TextInput::make('color')" => "TextInput::make('color')\n                    ->label('颜色')",
        "Textarea::make('description')" => "Textarea::make('description')\n                    ->label('描述')",
    ],

    // Users表单
    'app/Filament/Resources/Users/Schemas/UserForm.php' => [
        "TextInput::make('name')" => "TextInput::make('name')\n                    ->label('姓名')",
        "TextInput::make('email')" => "TextInput::make('email')\n                    ->label('邮箱')",
        "TextInput::make('username')" => "TextInput::make('username')\n                    ->label('用户名')",
        "DateTimePicker::make('email_verified_at')" => "DateTimePicker::make('email_verified_at')\n                    ->label('邮箱验证时间')",
        "TextInput::make('password')" => "TextInput::make('password')\n                    ->label('密码')",
        "TextInput::make('avatar')" => "TextInput::make('avatar')\n                    ->label('头像')",
        "Textarea::make('bio')" => "Textarea::make('bio')\n                    ->label('简介')",
        "Select::make('role')" => "Select::make('role')\n                    ->label('角色')",
        "Select::make('status')" => "Select::make('status')\n                    ->label('状态')",
        "DateTimePicker::make('last_login_at')" => "DateTimePicker::make('last_login_at')\n                    ->label('最后登录时间')",
    ],

    // Comments表单
    'app/Filament/Resources/Comments/Schemas/CommentForm.php' => [
        "Select::make('post_id')" => "Select::make('post_id')\n                    ->label('文章')",
        "Select::make('user_id')" => "Select::make('user_id')\n                    ->label('用户')",
        "Select::make('parent_id')" => "Select::make('parent_id')\n                    ->label('父级评论')",
        "Textarea::make('content')" => "Textarea::make('content')\n                    ->label('内容')",
        "TextInput::make('author_name')" => "TextInput::make('author_name')\n                    ->label('作者姓名')",
        "TextInput::make('author_email')" => "TextInput::make('author_email')\n                    ->label('作者邮箱')",
        "TextInput::make('author_url')" => "TextInput::make('author_url')\n                    ->label('作者网址')",
        "TextInput::make('author_ip')" => "TextInput::make('author_ip')\n                    ->label('作者IP')",
        "Select::make('status')" => "Select::make('status')\n                    ->label('状态')",
        "Toggle::make('is_featured')" => "Toggle::make('is_featured')\n                    ->label('特色评论')",
        "TextInput::make('likes_count')" => "TextInput::make('likes_count')\n                    ->label('点赞数')",
    ],
];

foreach ($formsToUpdate as $file => $replacements) {
    $filePath = __DIR__ . '/' . $file;

    if (!file_exists($filePath)) {
        echo "文件不存在: $filePath\n";
        continue;
    }

    $content = file_get_contents($filePath);
    $updatedContent = $content;

    foreach ($replacements as $search => $replace) {
        $updatedContent = str_replace($search, $replace, $updatedContent);
    }

    if ($updatedContent !== $content) {
        file_put_contents($filePath, $updatedContent);
        echo "✅ 已更新: $file\n";
    } else {
        echo "⚠️ 无需更新: $file\n";
    }
}

// 更新状态选项的中文
$statusUpdates = [
    'app/Filament/Resources/Users/Schemas/UserForm.php' => [
        "'admin' => 'Admin'" => "'admin' => '管理员'",
        "'editor' => 'Editor'" => "'editor' => '编辑'",
        "'author' => 'Author'" => "'author' => '作者'",
        "'user' => 'User'" => "'user' => '用户'",
        "'reader' => 'Reader'" => "'reader' => '读者'",
        "'active' => 'Active'" => "'active' => '活跃'",
        "'inactive' => 'Inactive'" => "'inactive' => '未激活'",
        "'banned' => 'Banned'" => "'banned' => '已封禁'",
    ],
    'app/Filament/Resources/Comments/Schemas/CommentForm.php' => [
        "'pending' => 'Pending'" => "'pending' => '待审核'",
        "'approved' => 'Approved'" => "'approved' => '已批准'",
        "'rejected' => 'Rejected'" => "'rejected' => '已拒绝'",
        "'spam' => 'Spam'" => "'spam' => '垃圾评论'",
    ],
];

foreach ($statusUpdates as $file => $replacements) {
    $filePath = __DIR__ . '/' . $file;

    if (!file_exists($filePath)) {
        continue;
    }

    $content = file_get_contents($filePath);
    $updatedContent = $content;

    foreach ($replacements as $search => $replace) {
        $updatedContent = str_replace($search, $replace, $updatedContent);
    }

    if ($updatedContent !== $content) {
        file_put_contents($filePath, $updatedContent);
        echo "✅ 已更新状态选项: $file\n";
    }
}

echo "\n所有表单字段标签更新完成！\n";